<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_setting extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'sys_setting';

		$this->primary_key = 'sys_setting.id_setting';

	}
	
	function get(){
		$this->load->database();
						
			$sql = "SELECT * FROM sys_setting";
			$query = $this->db->query($sql);
		
		$this->db->close();
		
		return $query;
	}
	
	function check($key){
		$check = false;
		$this->load->database();
			$sql="SELECT * FROM sys_setting WHERE name='$key'";
			$query = $this->db->query($sql);
			
			if($query->num_rows() > 0)
				$check = true;
		$this->db->close();
		
		return $check;
		
	}
	
	function edit($data){
		$this->load->database();
			$this->db->trans_start();
			foreach($data as $key=>$value){
				$record = array();
				$record['value'] = $value;
				
				if($this->check($key)){
					$this->db->where('name',$key);
					$this->db->update('sys_setting',$record);
				}else{
					$record['name'] = $key;
					$this->db->insert('sys_setting',$record);
				}
			}
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
			
		$this->db->close();
	}

}

?>